package com.pn.service.impl;

import com.pn.dto.AssignRoleDto;
import com.pn.entity.Result;
import com.pn.entity.User;
import com.pn.mapper.RoleMapper;
import com.pn.mapper.UserInfoMapper;
import com.pn.mapper.UserRoleMapper;
import com.pn.model.UserInfo;
import com.pn.model.UserRole;
import com.pn.page.Page;
import com.pn.service.UserInfoService;
import com.pn.utils.DigestUtil;
import com.pn.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public UserInfo queryUserInfoByCode(String userCode) {
        return userInfoMapper.findUserByCode(userCode);
    }

    //分页查询用户信息
    @Override
    public Page queryUserByPage(Page page, User user) {

        //查询用户行数
        Integer count = userInfoMapper.findUserRowCount(user);

        //分页查询用户
        List<User> userList = userInfoMapper.findUserByPage(page, user);

        //组装所有的分页信息
        page.setTotalNum(count);
        page.setResultList(userList);

        return page;
    }

    /**
     * 添加用户
     * @param userInfo
     * @return
     */
    @Override
    public Result saveUser(UserInfo userInfo) {

        //判断账号是否已经存在
        UserInfo u = userInfoMapper.findUserByCode(userInfo.getUserCode());
        if (u != null) {//账号已经存在
            return Result.err(Result.CODE_ERR_BUSINESS,"账号已存在");
        }

        //对密码进行加密处理
        String password = DigestUtil.hmacSign(userInfo.getUserPwd());
        userInfo.setUserPwd(password);

        //执行添加
        int i = userInfoMapper.insertUser(userInfo);
        if (i > 0) {
            return Result.ok("用户添加成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"用户添加失败");
    }

    /**
     * 修改用户状态:启用或者禁用用户
     * @param userInfo
     * @return
     */
    @Override
    public Result setUserState(UserInfo userInfo) {
        int i= userInfoMapper.setStateByUid(userInfo.getUserId(),userInfo.getUserState());
        if (i > 0) {
            return Result.ok("启用或禁用用户成功！");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"启用或禁用用户失败！");
    }

    /**
     * 给用户分配角色
     * @param assignRoleDto
     */
    //事务处理
    @Transactional
    @Override
    public void assignRole(AssignRoleDto assignRoleDto) {
        //删除用户已有权限
        userRoleMapper.removeUserRoleByUid(assignRoleDto.getUserId());

        List<String> roleNameList = assignRoleDto.getRoleCheckList();
        for (String roleName : roleNameList) {
            Integer roleId = roleMapper.findRoleIdByName(roleName);
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(assignRoleDto.getUserId());
            userRoleMapper.insertUserRole(userRole);

        }
    }

    /**
     * 删除用户
     * @param userIdlist
     * @return
     */
    @Override
    public Result removeUserByIds(List<Integer> userIdlist) {
        int i = userInfoMapper.setIsDeleteByUids(userIdlist);
        if(i>0){
            return Result.ok("用户删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"用户删除失败！");
    }

    /**
     * 修改用户
     * @param userInfo
     * @return
     */
    @Override
    public Result setUserById(UserInfo userInfo) {
        int i = userInfoMapper.setUserNameByUid(userInfo);
        if(i>0){
            return Result.ok("用户修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"用户修改失败！");
    }

    /**
     * 重置密码
     * @param userId
     * @return
     */
    @Override
    public Result setPwdById(Integer userId) {
        //给定初始妈咪123456并加密
        String password = DigestUtil.hmacSign("123456");

        //根据用户Id修改密码
        int i = userInfoMapper.setPwdByUid(userId,password);
        if(i>0){
            return Result.ok("密码重置成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"密码重置失败");
    }


}
